package cn.life.common.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
 * @Description:
 * @Author 李佳明
 * @Date 2018/6/24 0024.14:56.
 */
public class StringUtils {

    /**
     * 校验str不为空串
     */
    public static boolean isNullOrEmpty(String str) {
        int strLen;
        if(str != null && (strLen = str.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if(!Character.isWhitespace(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static String trim(String str) {
        return str == null?null:str.trim();
    }
    /**
     * 去除str串前后空格
     */
    public static String trimToNull(String str) {
        if(str == null) {
            return null;
        } else {
            str = str.trim();
            return str.length() == 0?null:str;
        }
    }
    /**
     * 如果str为空，则赋值空串，否则去除前后空格
     */
    public static String trimToEmpty(String str) {
        if(str == null) {
            return "";
        } else {
            str = str.trim();
            return str.length() == 0?"":str;
        }
    }

    /**
     * 生成UUID
     */
    public static String generateUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     *  判断str或者str2是否为空，否则返回str2在str一中的位置
     */
    public static int indexOf(String str, String str2) {
        return isNullOrEmpty(str)?-1:(isNullOrEmpty(str2)?-1:str.indexOf(str2));
    }

    public static int lastIndexOf(String str, String str2) {
        return isNullOrEmpty(str)?-1:(isNullOrEmpty(str2)?-1:str.lastIndexOf(str2));
    }

    public static String removeStart(String str, String str2) {
        int index = indexOf(str, str2);
        return index >= 0?str.substring(index):str;
    }

    public static String removeLast(String str, String str2) {
        int index = lastIndexOf(str, str2);
        return index >= 0?str.substring(0, index):str;
    }

    /**
     * 以regex分隔，将字符串转为
     */
    public static String[] splitToStrings(String str, String regex) {
        if(!isNullOrEmpty(str)) {
            String[] items = str.split(regex);
            if(items != null && items.length > 0) {
                return items;
            }
        }
        return null;
    }

    public static int[] splitToIntegers(String str, String regex) {
        int[] itemArray = null;
        if(!isNullOrEmpty(str)) {
            String[] items = str.split(regex);
            if(items != null && items.length > 0) {
                ArrayList list = new ArrayList();
                String[] i = items;
                int var6 = items.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    String item = i[var7];

                    try {
                        list.add(Integer.valueOf(Integer.parseInt(item)));
                    } catch (Exception var10) {
                        ;
                    }
                }

                if(list.size() > 0) {
                    itemArray = new int[list.size()];

                    for(int var11 = 0; var11 < itemArray.length; ++var11) {
                        itemArray[var11] = ((Integer)list.get(var11)).intValue();
                    }
                }
            }
        }

        return itemArray;
    }

    public static long[] splitToLongs(String str, String regex) {
        long[] itemArray = null;
        if(!isNullOrEmpty(str)) {
            String[] items = str.split(regex);
            if(items != null && items.length > 0) {
                ArrayList list = new ArrayList();
                String[] i = items;
                int var6 = items.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    String item = i[var7];

                    try {
                        list.add(Long.valueOf(Long.parseLong(item)));
                    } catch (Exception var10) {
                        ;
                    }
                }

                if(list.size() > 0) {
                    itemArray = new long[list.size()];

                    for(int var11 = 0; var11 < itemArray.length; ++var11) {
                        itemArray[var11] = ((Long)list.get(var11)).longValue();
                    }
                }
            }
        }

        return itemArray;
    }
    /**
     *  集合中的字符拼上separator，转为字符串
     */
    public static String combineString(Integer[] items, String separator) {
        if(items != null && items.length > 0) {
            StringBuffer sbResult = new StringBuffer();
            Integer[] var3 = items;
            int var4 = items.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Integer item = var3[var5];
                if(item != null) {
                    sbResult.append(item.intValue());
                    sbResult.append(separator);
                }
            }

            if(sbResult.length() > 0) {
                sbResult.deleteCharAt(sbResult.length() - 1);
            }

            return sbResult.toString();
        } else {
            return "";
        }
    }
    /**
     *  集合中的字符拼上separator，转为字符串
     */
    public static String combineString(int[] items, String separator) {
        if(items != null && items.length > 0) {
            StringBuffer sbResult = new StringBuffer();
            int[] var3 = items;
            int var4 = items.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                int item = var3[var5];
                sbResult.append(item);
                sbResult.append(separator);
            }

            sbResult.deleteCharAt(sbResult.length() - 1);
            return sbResult.toString();
        } else {
            return "";
        }
    }
    /**
     *  集合中的字符拼上separator，转为字符串
     */
    public static String combineString(Long[] items, String separator) {
        if(items != null && items.length > 0) {
            StringBuffer sbResult = new StringBuffer();
            Long[] var3 = items;
            int var4 = items.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Long item = var3[var5];
                if(item != null) {
                    sbResult.append(item.longValue());
                    sbResult.append(separator);
                }
            }

            if(sbResult.length() > 0) {
                sbResult.deleteCharAt(sbResult.length() - 1);
            }

            return sbResult.toString();
        } else {
            return "";
        }
    }
    /**
     *  集合中的字符拼上separator，转为字符串
     */
    public static String combineString(long[] items, String separator) {
        if(items != null && items.length > 0) {
            StringBuffer sbResult = new StringBuffer();
            long[] var3 = items;
            int var4 = items.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                long item = var3[var5];
                sbResult.append(item);
                sbResult.append(separator);
            }

            sbResult.deleteCharAt(sbResult.length() - 1);
            return sbResult.toString();
        } else {
            return "";
        }
    }
    /**
     *  集合中的字符拼上separator，转为字符串
     */
    public static <T> String combineString(List<T> items, String separator) {
        if(items != null && items.size() > 0) {
            StringBuffer sbResult = new StringBuffer();
            Iterator var3 = items.iterator();

            while(var3.hasNext()) {
                Object item = var3.next();
                if(item != null) {
                    sbResult.append(item);
                    sbResult.append(separator);
                }
            }

            if(sbResult.length() > 0) {
                sbResult.deleteCharAt(sbResult.length() - 1);
            }

            return sbResult.toString();
        } else {
            return "";
        }
    }


    /**
     * 阿拉伯数字转为中文
     */
    public static String intToChineseNumber(int decimal) {
        String[] numArray = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};
        char[] val = String.valueOf(decimal).toCharArray();
        int len = val.length;
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < len; ++i) {
            int n = Integer.valueOf(val[i] + "").intValue();
            sb.append(numArray[n]);
        }

        return sb.toString();
    }


}
